package com.ruizhong.centercontroll;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

public class ScreenRecordService extends Service {
    private ScreenLive screenLive;
    private String url = "rtmp://192.168.0.207:1935/live/livestream";
    private static final String TAG = "ScreenShortRecordService";

    private static final String NOTIFICATION_CHANNEL_ID = "com.zhn.learn_android.MediaService";
    private static final String NOTIFICATION_CHANNEL_NAME = "com.zhn.learn_android.channel_name";
    private static final String NOTIFICATION_CHANNEL_DESC = "com.zhn.learn_android.channel_desc";
    private MediaProjection mediaProjection;

    public void startRecorder(MediaProjectionManager mediaProjectionManager, int resultCode, Intent data) {
        startNotification();
        // 创建一个MediaProjection回调
        MediaProjection.Callback callback = new MediaProjection.Callback() {
            @Override
            public void onStop() {
                super.onStop();
                screenLive.stopLive();
            }
        };
        mediaProjection = mediaProjectionManager.getMediaProjection
                (resultCode, data);
        if (mediaProjection != null) {
            // 注册回调
            mediaProjection.registerCallback(callback, null);
            // 现在可以安全地开始捕获屏幕内容了
            // 例如：创建VirtualDisplay
        }
        screenLive = new ScreenLive();
        screenLive.startLive(url, mediaProjection);
    }

    private void startNotification() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Intent notificationIntent = new Intent(this, ScreenRecordService.class);

            PendingIntent pendingIntent =
                    (Build.VERSION.SDK_INT >= 31) ?
                            PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE) :
                            PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE);

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("Starting Service")
                    .setContentText("Starting monitoring service")
                    .setContentIntent(pendingIntent);

            Notification notification = notificationBuilder.build();
            NotificationChannel channel = new NotificationChannel(
                    NOTIFICATION_CHANNEL_ID,
                    NOTIFICATION_CHANNEL_NAME,
                    NotificationManager.IMPORTANCE_DEFAULT
            );
            channel.setDescription(NOTIFICATION_CHANNEL_DESC);
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(channel);

            // Request to become a foreground service
            startForeground(1, notification);
        }
    }

    public class ScreenRecordBinder extends Binder {
        public ScreenRecordService getService() {
            return ScreenRecordService.this;
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new ScreenRecordBinder();
    }
}
